<template>
  <div class="list-page">
    <Row type="flex" align="middle" style="margin-bottom: 10px;">
        说明：登录页顶部展示
        <div class="demo-upload-list">
            <img :src="imgUrl" fit="cover" width="100%" height="100%" />
            <div class="demo-upload-list-cover">
                <Icon type="ios-eye-outline" @click="handleView()"></Icon>
            </div>
        </div>
        <Modal title="广告图" v-model="visible">
            <img :src="imgUrl" v-if="visible" style="width: 100%">
        </Modal>
    </Row>
    <Row>
      <div class="toolbar">
        <router-link :to="{ path: '/banners/adv/add' }">
          <Button
            type="primary"
            icon="md-add"
            style="margin: 0 10px"
            v-if="Validation.check(Resources.BANNERS_ADD)"
          >新建广告图
          </Button>
        </router-link>
        <Input
          placeholder="请输入广告名称"
          v-model="bloc.form.bannername"
          style="width: 180px; margin-right: 10px"
        />

        <Button
          type="primary"
          icon="ios-search"
          style="margin: 0 10px"
          size="default"
          @click="bloc.load(1, bloc.paged.pageSize)"
        >查询
        </Button>
        <Button
          icon="md-refresh"
          style="margin-right: 10px"
          size="default"
          @click="bloc.reset()"
        >重置</Button
        >
      </div>
    </Row>
    <Row>
      <Table
        border
        :loading="bloc.loading"
        :columns="columns"
        highlight-row
        :data="bloc.paged.items"
      >
        <template slot-scope="{ row }" slot="action">
          <div style="white-space: nowrap">
            <Button
              size="small"
              @click="modifyItem(row)"
              v-if="
                            Validation.check(Resources.BANNERS_UPDATE)
                          "
              type="info"
            >编辑
            </Button>
            <Button
              size="small"
              @click="removeItem(row)"
              v-if="
                            Validation.check(Resources.BANNERS_DELETE)
                          "
              type="warning"
            >删除
            </Button>
         </div>
        </template>
        <slot name="footer" slot="footer">
          <Page
            show-elevator
            show-total
            show-sizer
            :total="bloc.paged.itemCount"
            :current="bloc.paged.pageIndex"
            :page-size="bloc.paged.pageSize"
            :page-size-opts="[15, 30, 50, 100]"
            @on-change="(val) => bloc.load(val, bloc.paged.pageSize)"
            @on-page-size-change="(val) => bloc.load(bloc.paged.pageIndex, val)"
          />
        </slot>
      </Table>
    </Row>
  </div>
</template>

<script lang="ts">
    import {Component, Vue} from 'vue-property-decorator';
    import { Route } from "vue-router";
    import { AdvBloc } from "@/view/banners/AdvBloc";
    import { Adv } from "@/base/model/Adv";
    import { formatDate } from "@/base/util/Dates";
    import { Validation } from "@/base/Validation";
    import { Resources } from "@/base/Resources";
    import { store } from '@/store/Store';

    const columns = (bloc: AdvBloc) => [
        {
            title: "广告图",
            key: "imageurl",
            align: "left",
            width: 200,
            render: (h: any, params: any) => {
                return h("div", [
                    h("img", {
                        domProps: {
                            src: params.row.imageurl,
                        },
                        style: {
                            width: "50px",
                            height: "50px",
                        },
                    }),
                ]);
            },
            tooltip: true,
        },
        {
            title: "广告图名称",
            key: "bannername",
            align: "left",
            minWidth: 50,
            tooltip: true,
        },

        // {
        //     title: "轮播页归属",
        //     key: "bannerid",
        //     render: (h: any, params: any) => {
        //         return h("span", {
        //             domProps: {
        //                 innerText: params.row.companyid === 0 ?
        //                     params.row.agentname
        //                     :params.row.companyname,
        //             },
        //         });
        //     },
        //     align: "left",
        //     minWidth: 220,
        //     tooltip: true,
        // },
        // {
        //     title: "归属人类型",
        //     key: "bannerid",
        //     render: (h: any, params: any) => {
        //         return h("span", {
        //             domProps: {
        //                 innerText: params.row.companyid === 0 ?
        //                     "经销商"
        //                     :"品牌商",
        //             },
        //         });
        //     },
        //     align: "left",
        //     minWidth: 220,
        //     tooltip: true,
        // },
        {
            title: '开启设置',
            key: 'status',
            render: (h: any, params: any) => {
                return h('div', [
                    h(
                        'i-switch',
                        {
                            props: {
                                type: 'primary',
                                size: 'small',
                                title: '状态',
                                disabled: ( params.row.status === 0 && !Validation.check(Resources.BANNERS_UPDATE)),
                                value: params.row.status === 1
                            },
                            style: {
                                marginRight: '2px'
                            },
                            on: {
                                'on-change': value => {
                                    bloc.switchStatus(params.row.bannerid, value, params.index);
                                }
                            }
                        },
                        'selected'
                    )
                ]);
            },
            tooltip: true,
            width: 80
        },
        {
            title: "修改时间",
            render: (h: any, params: any) => {
                return h("span", {
                    domProps: {
                        innerText: formatDate(
                            new Date(params.row.updatetime),
                            "yyyy-MM-dd hh:mm:ss"
                        ),
                    },
                });
            },
            width: 200,
            align: "left",
            tooltip: true,
        },
        {
            title: "操作",
            slot: "action",
            width: 200,
            align: "left",
            fixed: "right",
        },
    ];

    @Component
    export default class extends Vue {
        beforeRouteEnter(to: Route, from: Route, next: (vm: any) => void) {
            next((vm) => {
                vm.bloc.type = vm.tepee;
                vm.bloc
                    .load(vm.bloc.paged.pageIndex, vm.bloc.paged.pageSize)
                    .catch((e) => {
                        vm.$Message.error("操作错误");
                    });
            });
        }
        bloc: AdvBloc = new AdvBloc();
        Validation = Validation;
        Resources = Resources;
        columns = columns(this.bloc);

        public row: any;
        imgUrl='https://irest.oss-cn-hangzhou.aliyuncs.com/image/1555551032967200.png';
        visible=false;
        removeItem(row) {
            this.row = row;
            this.$Modal.confirm({
                title: "提示",
                content: "确认删除后将无法恢复！还要继续吗？",
                okText: "删除",
                cancelText: "取消",
                onOk: () => {
                    this.bloc.removeItem(row)
                        .then((res) => {
                            if (res.name === '') {
                                this.$Message.success('删除成功');
                            } else {
                                this.$Message.error('删除失败');
                            }
                        }).finally(() => {
                        this.bloc.reset();
                    });
                },
            });
        }

        modifyItem(dataItem:Adv) {
            this.$router.push({
                path: '/banners/nav/adv/edit/'+dataItem.bannerid});
        }
        // switchStatus(id, value, index) {
        //
        //             if (value) {
        //                 this.bloc.form.status = 1
        //             } else {
        //                 this.bloc.form.status = 0
        //             }
        //             this.bloc.modifyItem(this.bloc.form.status,id)
        //
        // }
        get tepee() {
            return store.state.user.tepee
        }
        handleView() {
            this.visible = true;
        }
    }

</script>

<style lang="less">
</style>
